package com.sike.netty.im.client.handler;

import com.sike.netty.im.client.event.CustomEvent;
import com.sike.netty.im.client.event.EventFactory;
import com.sike.netty.im.client.event.EventTypeEnum;
import com.sike.netty.im.client.service.BeanFactory;
import com.sike.netty.im.client.service.LoginService;
import com.sike.netty.im.common.command.LoginResponseCommand;
import com.sike.netty.im.common.enums.ResponseStatusEnum;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

import java.util.Scanner;


/**
 * 登录响应 handler
 *
 * @author chenssy
 * @date 2023/09/15
 */
@Slf4j
public class LoginResponseHandler extends SimpleChannelInboundHandler<LoginResponseCommand> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, LoginResponseCommand msg) throws Exception {
        if (ResponseStatusEnum.FAILURE.getCode().equals(msg.getRetCode())) {
           log.error("{}",msg.getRetMessage());
           // 登录失败，重新登录
            LoginService loginService = (LoginService) BeanFactory.getServiceBean(LoginService.class);
            loginService.login(ctx.channel());
        } else {
            log.info("登录成功，进入主菜单界面");

            // 主菜单事件
            CustomEvent customEvent = EventFactory.getCustomEvent(EventTypeEnum.MAIN_MENU_EVENT.getEvent());
            ctx.fireUserEventTriggered(customEvent);
        }
    }
}
